package com.douyin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.douyin.dto.CommentAddDto;
import com.douyin.dto.Result;
import com.douyin.entity.Comments;
import com.douyin.service.CommentsService;
import com.douyin.utils.BaseContext;
import com.douyin.vo.CommentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 视频评论表 前端控制器
 * </p>
 *
 * @author k
 * @since 2025-07-27
 */
@Slf4j
@RestController
@RequestMapping("/comments")
public class CommentsController {
    @Autowired
    CommentsService commentsService;
    @PostMapping("/add")
    public Result addComment(@RequestBody CommentAddDto commentAddDto) {
        log.info("addComment:{}",commentAddDto);
        return commentsService.addComment(commentAddDto);
    }
    @GetMapping("/{videoId}/topComments")
    public Result getTopComments(
            @PathVariable Long videoId,
            @RequestParam(defaultValue = "1") long current, // 当前页码
            @RequestParam(defaultValue = "10") long size     // 每页数量
    ) {
        Long currentUserId = BaseContext.getCurrentId(); // 示例：从安全上下文中获取
        IPage<CommentVO> page = new Page<>(current, size);
        IPage<CommentVO> topComments = commentsService.getTopComments(page, videoId, currentUserId);
        if (topComments == null) {
            return Result.fail("查询失败");
        }
        return Result.ok(topComments);
    }
    @GetMapping("/replies")
    public Result getAllRepliesForComment(Long commentId,Long videoId) {
        Long currentUserId = BaseContext.getCurrentId();
        List<CommentVO> replies = commentsService.getAllReplies(commentId, currentUserId,videoId);
        return Result.ok(replies);
    }

    @GetMapping("/{id}")
    public Comments getCommentById(@PathVariable Long id) {
        log.info("内部服务根据id查询评论");
        return commentsService.getById(id);
    }
    @PutMapping("/update")
    public Result updateByCommentId(@RequestBody Comments comments){
        log.info("内部服务根据id更改评论");
        boolean b = commentsService.updateById(comments);
        if (b){
            return Result.ok();
        }
        return Result.fail("更改失败");
    }
}

